package com.huanwang.commonlibrary.base

import android.app.Application
import androidx.annotation.NonNull
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.huanwang.commonlibrary.utils.ReflectUtils
import com.tqzhang.stateview.core.LoadManager




abstract class BaseViewModel< T : BaseModuleRepository> (@NonNull application: Application) : AndroidViewModel(application){
    var mRepository: T? = null
    var liveDataS : HashMap<String,MutableLiveData<*>>? = hashMapOf<String,MutableLiveData<*>>();

    init {
        mRepository = ReflectUtils.getNewInstance(this, 0)

    }

    /**
     * 返回数据类型的监听对象，泛型重复的获取的是同一个对象；需注意
     * @param mutableKey  保存对应bean类的MutableLiveData 对象
     *@return     反回对应bean类的MutableLiveData 对象
     */
    fun <E> getMutableLiveData(mutableKey : Class<E>): MutableLiveData<E>? {
         if (!liveDataS!!.containsKey(mutableKey.name)){
             liveDataS!!.put(mutableKey.name, MutableLiveData<E>())
         }
        return mutableKey?.let {  liveDataS!!.get(mutableKey.name) as MutableLiveData<E> }
    }


    /**
     * @exception Activity销毁时清除内存
     */
    override fun onCleared() {
        super.onCleared()
        liveDataS!!.clear()
        liveDataS = null
        mRepository!!.unSubscribe()
        mRepository=null
    }

}